package com.ahzx.banner.service.impl;

import com.ahzx.banner.domain.Banner;
import com.ahzx.banner.mapper.BannerMapper;
import com.ahzx.banner.service.BannerService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * banner管理业务实现层
 *
 * @Author syg
 * @Date 2021/4/27 16:25
 * @Desc To be or not to be,that is a question.
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private BannerMapper bannerMapper;

    @Value("${sftp.bannerPath}")
    private String bannerPath;
    @Value("${spring.application.name}")
    private String applicationName;

    @Override
    public List<Banner> selectBannerList() {
        return bannerMapper.selectBannerList();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertBanner(Banner banner) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        banner.setBannerUrlPrefix(bannerPath);
        banner.setCreateBy(loginUser.getUsername());
        banner.setBannerResourceUrl(applicationName + "/resource");
        bannerMapper.insertBanner(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBannerById(Banner banner) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        banner.setUpdateBy(loginUser.getUsername());
        bannerMapper.updateBannerById(banner);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBannerById(Integer id) {
        bannerMapper.deleteBannerById(id);
    }
}
